package com.xoa.util.netdisk;

import java.io.*;
import java.nio.channels.FileChannel;

public class CopyFile {
    public static String readByBufferedReader(String fileName) {  
    	 String read = null;  
    	 String read1=null;
    	try {  
            File file = new File(fileName);  
            // 读取文件，并且以utf-8的形式写出去  
            BufferedReader bufread;
            InputStreamReader isr;
            byte[] b = new byte[3];
            if (b[0] == -17 && b[1] == -69 && b[2] == -65){
           isr = new InputStreamReader(new FileInputStream(file), "GBK");}
            else{
            isr = new InputStreamReader(new FileInputStream(file), "GBK");}
           
           // InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
                bufread = new BufferedReader(isr);
           // bufread = new BufferedReader(new FileReader(file));  
            while ((read = bufread.readLine()) != null) {
                read1=read;
            }  
            bufread.close();  
        } catch (FileNotFoundException ex) {  
            ex.printStackTrace();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }
		return read1;  
    }
    
    public static  void writeFile(String content,File file ) {  
    	  // 向文件写入内容(输出流)  
        String str = content;  
        byte bt[] = new byte[1024];  
        bt = str.getBytes();  
        try {  
            FileOutputStream in = new FileOutputStream(file);  
            try {  
                in.write(bt, 0, bt.length);  
                in.close();  
                // boolean success=true;  
                // System.out.println("写入文件成功");  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  

    }

    public static void copeFile(File srcFile,File destFile){
        // 复制文件
        int byteread = 0; // 读取的字节数
        FileChannel in = null;
        FileChannel out = null;
        FileInputStream inStream = null;
        FileOutputStream outStream = null;
        try {
            inStream = new FileInputStream(srcFile);
            outStream = new FileOutputStream(destFile);
            in = inStream.getChannel();
            out = outStream.getChannel();
            in.transferTo(0, in.size(), out);
        } catch (FileNotFoundException e) {
         e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outStream != null)
                    outStream.close();
                if (inStream != null)
                    inStream.close();
                if (out != null)
                    out.close();
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
}
